package com.phpxiu.app.presenters;

import android.support.annotation.Nullable;
import android.util.Log;

import com.phpxiu.app.presenters.viewinterface.C2CChatView;
import com.phpxiu.app.utils.KKYUtil;
import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMManager;
import com.tencent.TIMMessage;
import com.tencent.TIMMessageListener;
import com.tencent.TIMValueCallBack;
import com.tencent.bugly.imsdk.crashreport.CrashReport;

import java.util.List;

/**
 * Created by 吴金洲 on 2016/7/25
 */
@Deprecated
public class C2CChatPresenter implements TIMMessageListener {
    private final static String TAG = "C2CChatPresenter";
    private C2CChatView view;
    private TIMConversation conversation;
    private final int LAST_MESSAGE_NUM = 20;
    public C2CChatPresenter(C2CChatView view,String identify,TIMConversationType type){
        this.view = view;
        conversation = TIMManager.getInstance().getConversation(type,identify);
        conversation.setReadMessage();
    }

    /**
     * 加载页面逻辑
     */
    public void start() {
        //EventBus.getDefault().post(new OnC2CRead((int)conversation.getUnreadMessageNum()));//广播已读会话
        TIMManager.getInstance().addMessageListener(this);
        //注册消息监听
        getMessage(null);
    }

    public void stop(){
        TIMManager.getInstance().removeMessageListener(this);
    }

    /**
     * 获取聊天TIM会话
     */
    public TIMConversation getConversation(){
        return conversation;
    }

    @Override
    public boolean onNewMessages(List<TIMMessage> timMessages) {
        try {
            for (TIMMessage m:timMessages){
                KKYUtil.log("收到新消息，显示会话消息"+conversation.getPeer());
                if(m.getConversation().getPeer().equals(conversation.getPeer())&&m.getConversation().getType()==TIMConversationType.C2C){
                    view.showMessage(timMessages.get(0));
                }
            }
            readMessages();
        }catch (NullPointerException e){
            e.printStackTrace();
            CrashReport.postCatchedException(e);
        }
        return false;
    }

    /**
     * 发送消息
     *
     * @param message 发送的消息
     */
    public void sendMessage(final TIMMessage message) {
        view.showMessage(message);
        conversation.sendMessage(message, new TIMValueCallBack<TIMMessage>() {
            @Override
            public void onError(int code, String desc) {//发送消息失败
                //错误码code和错误描述desc，可用于定位请求失败原因
                //错误码code含义请参见错误码表
                view.onSendMessageFail(code, desc,message);
            }

            @Override
            public void onSuccess(TIMMessage msg) {
                //发送消息成功,消息状态已在sdk中修改，此时只需更新界面,通知更新会话列表
                view.onSendMessageSuccess(message);
            }
        });
    }

    /**
     * 获取最新n消息
     *
     * @param message 最后一条消息
     */
    public void getMessage(@Nullable TIMMessage message){
        conversation.getMessage(LAST_MESSAGE_NUM, message, new TIMValueCallBack<List<TIMMessage>>() {
            @Override
            public void onError(int i, String s) {
                KKYUtil.log("get message error" + s);
            }

            @Override
            public void onSuccess(List<TIMMessage> timMessages) {
                KKYUtil.log("显示会话消息"+conversation.getPeer()+"###"+timMessages.size());
                view.showMessage(timMessages);
            }
        });
    }

    /**
     * 设置会话为已读
     */
    public void readMessages(){
        conversation.setReadMessage();
    }

}
